finp$="1.png"
ysz=80:xsz=80

if argc>2 then:finp$=argv(2):end if
if argc>3 then:ysz=val(argv(3)):end if
ysz2=ysz
if ysz2<50 then:ysz2=50:end if

open finp$+".ans" for output as #1

setdisplay(160,ysz2,32,1):paper(8^8-1):ink(0):pen(0):cls
loadimage(finp$,1):pasteicon(80,0,1)
loadimage("palette_3b.png",1):pasteicon(0,0,1)

dim och[256]
och[0]=0x20
och[1]=0xB0
och[2]=0xB1
och[3]=0xB2
och[4]=0xDB
och[5]=0xDF
och[6]=0xDC

dim plt[256]
plt[0]=0
plt[1]=4
plt[2]=1
plt[3]=5
plt[4]=2
plt[5]=6
plt[6]=3
plt[7]=7

function euclidistrgb(rq1,gq1,bq1,rq2,gq2,bq2)
  return sqr( ((abs (rq2-rq1))^2)+((abs (gq2-gq1))^2)+((abs (bq2-bq1))^2) )
  end function

for ypi=0 to ysz-1 step 2
  for xpi=0 to xsz-1
    setcaption(str$(ysz-ypi))
    c0=point(80+xpi,ypi)
    c1=point(80+xpi,ypi+1)
    b0=(c0 mod 256):c0=int(c0/256)
    g0=(c0 mod 256):c0=int(c0/256)
    r0=(c0 mod 256)
    b1=(c1 mod 256):c1=int(c1/256)
    g1=(c1 mod 256):c1=int(c1/256)
    r1=(c1 mod 256)
    xps=0:yps=0:mdt=10000000
    for ypl=0 to 31 step 2
      for xpl=0 to 55
        c2=point(xpl,ypl)
        c3=point(xpl,ypl+1)
        b2=(c2 mod 256):c2=int(c2/256)
        g2=(c2 mod 256):c2=int(c2/256)
        r2=(c2 mod 256)
        b3=(c3 mod 256):c3=int(c3/256)
        g3=(c3 mod 256):c3=int(c3/256)
        r3=(c3 mod 256)
        d0=euclidistrgb(r0,g0,b0,r2,g2,b2)
        d1=euclidistrgb(r1,g1,b1,r3,g3,b3)
        d2=(d0+d1)/2
        if d2<mdt then
          mdt=d2
          xps=xpl
          yps=ypl
          end if
        next:next
    xps0=(xps mod 7)                'chr
    xps1=(int(xps/7)) mod 8         'ink
    yps0=(int((yps mod 16)/2))mod 8 'ppr
    yps1=(int(yps/16))mod 2         'brh
    writebyte(1,0x1B)
    writebyte(1,0x5B)
    writebyte(1,0x30+yps1)
    writebyte(1,0x3B)
    writebyte(1,0x33)
    writebyte(1,0x30+plt[xps1])
    writebyte(1,0x3B)
    writebyte(1,0x34)
    writebyte(1,0x30+plt[yps0])
    writebyte(1,0x3B)
    writebyte(1,0x6D)
    writebyte(1,och[xps0])
    next:next
close #1

